package com.ruoyi.Applet.controller.parts;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.system.domain.CarInfo;
import com.ruoyi.system.domain.vo.carInfo.CarInfoListVo;
import com.ruoyi.system.domain.vo.carInfo.CarSpellDictVo;
import com.ruoyi.system.domain.vo.eCatalog.ECatalogCarVo;
import com.ruoyi.system.domain.vo.eCatalog.ECatalogVo;
import com.ruoyi.system.service.ICarInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName : CarController
 * @Description :
 * @Author :
 * @Date: 2023-01-26 23:13
 */

@RestController
@RequestMapping("/applet/parts/carInfo")
public class CarController extends BaseController {

    @Autowired
    private ICarInfoService carInfoService;

    /**
     * 查询车型热门前十
     */
    @PreAuthorize("@ss.hasPermi('system:carinfo:list')")
    @GetMapping("/getHotDict")
    public TableDataInfo getHotDict(CarInfo carInfo)
    {
        startPage();
        List<CarSpellDictVo> list = carInfoService.getHotDict(carInfo);
        List<String> collect = list.stream().distinct()
                .map(lis -> lis.getBrand())
                .collect(Collectors.toList());
        return getDataTable(collect);
    }

    /**
     * 查询车型拼音列表
     */
    @PreAuthorize("@ss.hasPermi('system:carinfo:list')")
    @GetMapping("/getCarSpell")
    public AjaxResult getCarSpell(CarInfo carInfo)
    {
//        startPage();
        List<CarInfo> list = carInfoService.selectCarInfoList(null);
        List<CarSpellDictVo> dictVos = CollectionUtils.convertCollectionType(list, CarSpellDictVo.class);
        List<CarSpellDictVo> collect = dictVos.stream().distinct().collect(Collectors.toList());
        Map<String, List<String>> spellList = collect.stream()
                .collect(Collectors.groupingBy(
                        CarSpellDictVo::getFisrtSpell,
                        Collectors.mapping(CarSpellDictVo::getBrand, Collectors.toList())
                ));
        return success(spellList);
    }


    /**
     * 查询汽车信息
     */
    @PreAuthorize("@ss.hasPermi('system:carinfo:list')")
    @GetMapping("/getECatalogCar")
    public AjaxResult getECatalogCar(ECatalogVo eCatalogVo)
    {
//        startPage();
        CarInfo carInfo = carInfoService.getECatalogCar(eCatalogVo);
        ECatalogCarVo eCatalogCarVo = new ECatalogCarVo();
        BeanUtil.copyProperties(carInfo,eCatalogCarVo);
        return success(eCatalogCarVo);
    }

    /**
     * 查询对应产品的所有适配汽车
     */
    @PreAuthorize("@ss.hasPermi('system:carinfo:list')")
    @GetMapping("/getApplicableModel")
    public AjaxResult getApplicableModel(@RequestParam("id") Long productId)
    {
//        startPage();
        List<CarInfo> carInfoList = carInfoService.getApplicableModel(productId);
        List<ECatalogCarVo> eCatalogCarVos = CollectionUtils.convertCollectionType(carInfoList, ECatalogCarVo.class);
        return success(eCatalogCarVos);
    }

}
